package permission_pb

import (
	context "context"
	fmt "fmt"
	gorm1 "github.com/infobloxopen/atlas-app-toolkit/gorm"
	query "github.com/infobloxopen/atlas-app-toolkit/query"
	errors "github.com/infobloxopen/protoc-gen-gorm/errors"
	gorm "github.com/jinzhu/gorm"
	pq "github.com/lib/pq"
	field_mask "google.golang.org/genproto/protobuf/field_mask"
	timestamppb "google.golang.org/protobuf/types/known/timestamppb"
	strings "strings"
	time "time"
)

type AuthorityORM struct {
	AuthorityName string
	CreatedAt     *time.Time
	DefaultRouter string
	Id            uint64
	MenuIds       pq.Int64Array `gorm:"type:integer[]"`
	ParentId      string
	UpdatedAt     *time.Time
}

// TableName overrides the default tablename generated by GORM
func (AuthorityORM) TableName() string {
	return "authorities"
}

// ToORM runs the BeforeToORM hook if present, converts the fields of this
// object to ORM format, runs the AfterToORM hook, then returns the ORM object
func (m *Authority) ToORM(ctx context.Context) (AuthorityORM, error) {
	to := AuthorityORM{}
	var err error
	if prehook, ok := interface{}(m).(AuthorityWithBeforeToORM); ok {
		if err = prehook.BeforeToORM(ctx, &to); err != nil {
			return to, err
		}
	}
	if m.CreatedAt != nil {
		t := m.CreatedAt.AsTime()
		to.CreatedAt = &t
	}
	if m.UpdatedAt != nil {
		t := m.UpdatedAt.AsTime()
		to.UpdatedAt = &t
	}
	to.Id = m.Id
	to.AuthorityName = m.AuthorityName
	to.ParentId = m.ParentId
	to.DefaultRouter = m.DefaultRouter
	if m.MenuIds != nil {
		to.MenuIds = make(pq.Int64Array, len(m.MenuIds))
		copy(to.MenuIds, m.MenuIds)
	}
	if posthook, ok := interface{}(m).(AuthorityWithAfterToORM); ok {
		err = posthook.AfterToORM(ctx, &to)
	}
	return to, err
}

// ToPB runs the BeforeToPB hook if present, converts the fields of this
// object to PB format, runs the AfterToPB hook, then returns the PB object
func (m *AuthorityORM) ToPB(ctx context.Context) (Authority, error) {
	to := Authority{}
	var err error
	if prehook, ok := interface{}(m).(AuthorityWithBeforeToPB); ok {
		if err = prehook.BeforeToPB(ctx, &to); err != nil {
			return to, err
		}
	}
	if m.CreatedAt != nil {
		to.CreatedAt = timestamppb.New(*m.CreatedAt)
	}
	if m.UpdatedAt != nil {
		to.UpdatedAt = timestamppb.New(*m.UpdatedAt)
	}
	to.Id = m.Id
	to.AuthorityName = m.AuthorityName
	to.ParentId = m.ParentId
	to.DefaultRouter = m.DefaultRouter
	if m.MenuIds != nil {
		to.MenuIds = make(pq.Int64Array, len(m.MenuIds))
		copy(to.MenuIds, m.MenuIds)
	}
	if posthook, ok := interface{}(m).(AuthorityWithAfterToPB); ok {
		err = posthook.AfterToPB(ctx, &to)
	}
	return to, err
}

// The following are interfaces you can implement for special behavior during ORM/PB conversions
// of type Authority the arg will be the target, the caller the one being converted from

// AuthorityBeforeToORM called before default ToORM code
type AuthorityWithBeforeToORM interface {
	BeforeToORM(context.Context, *AuthorityORM) error
}

// AuthorityAfterToORM called after default ToORM code
type AuthorityWithAfterToORM interface {
	AfterToORM(context.Context, *AuthorityORM) error
}

// AuthorityBeforeToPB called before default ToPB code
type AuthorityWithBeforeToPB interface {
	BeforeToPB(context.Context, *Authority) error
}

// AuthorityAfterToPB called after default ToPB code
type AuthorityWithAfterToPB interface {
	AfterToPB(context.Context, *Authority) error
}

// DefaultCreateAuthority executes a basic gorm create call
func DefaultCreateAuthority(ctx context.Context, in *Authority, db *gorm.DB) (*Authority, error) {
	if in == nil {
		return nil, errors.NilArgumentError
	}
	ormObj, err := in.ToORM(ctx)
	if err != nil {
		return nil, err
	}
	if hook, ok := interface{}(&ormObj).(AuthorityORMWithBeforeCreate_); ok {
		if db, err = hook.BeforeCreate_(ctx, db); err != nil {
			return nil, err
		}
	}
	if err = db.Create(&ormObj).Error; err != nil {
		return nil, err
	}
	if hook, ok := interface{}(&ormObj).(AuthorityORMWithAfterCreate_); ok {
		if err = hook.AfterCreate_(ctx, db); err != nil {
			return nil, err
		}
	}
	pbResponse, err := ormObj.ToPB(ctx)
	return &pbResponse, err
}

type AuthorityORMWithBeforeCreate_ interface {
	BeforeCreate_(context.Context, *gorm.DB) (*gorm.DB, error)
}
type AuthorityORMWithAfterCreate_ interface {
	AfterCreate_(context.Context, *gorm.DB) error
}

func DefaultReadAuthority(ctx context.Context, in *Authority, db *gorm.DB) (*Authority, error) {
	if in == nil {
		return nil, errors.NilArgumentError
	}
	ormObj, err := in.ToORM(ctx)
	if err != nil {
		return nil, err
	}
	if ormObj.Id == 0 {
		return nil, errors.EmptyIdError
	}
	if hook, ok := interface{}(&ormObj).(AuthorityORMWithBeforeReadApplyQuery); ok {
		if db, err = hook.BeforeReadApplyQuery(ctx, db); err != nil {
			return nil, err
		}
	}
	if db, err = gorm1.ApplyFieldSelection(ctx, db, nil, &AuthorityORM{}); err != nil {
		return nil, err
	}
	if hook, ok := interface{}(&ormObj).(AuthorityORMWithBeforeReadFind); ok {
		if db, err = hook.BeforeReadFind(ctx, db); err != nil {
			return nil, err
		}
	}
	ormResponse := AuthorityORM{}
	if err = db.Where(&ormObj).First(&ormResponse).Error; err != nil {
		return nil, err
	}
	if hook, ok := interface{}(&ormResponse).(AuthorityORMWithAfterReadFind); ok {
		if err = hook.AfterReadFind(ctx, db); err != nil {
			return nil, err
		}
	}
	pbResponse, err := ormResponse.ToPB(ctx)
	return &pbResponse, err
}

type AuthorityORMWithBeforeReadApplyQuery interface {
	BeforeReadApplyQuery(context.Context, *gorm.DB) (*gorm.DB, error)
}
type AuthorityORMWithBeforeReadFind interface {
	BeforeReadFind(context.Context, *gorm.DB) (*gorm.DB, error)
}
type AuthorityORMWithAfterReadFind interface {
	AfterReadFind(context.Context, *gorm.DB) error
}

func DefaultDeleteAuthority(ctx context.Context, in *Authority, db *gorm.DB) error {
	if in == nil {
		return errors.NilArgumentError
	}
	ormObj, err := in.ToORM(ctx)
	if err != nil {
		return err
	}
	if ormObj.Id == 0 {
		return errors.EmptyIdError
	}
	if hook, ok := interface{}(&ormObj).(AuthorityORMWithBeforeDelete_); ok {
		if db, err = hook.BeforeDelete_(ctx, db); err != nil {
			return err
		}
	}
	err = db.Where(&ormObj).Delete(&AuthorityORM{}).Error
	if err != nil {
		return err
	}
	if hook, ok := interface{}(&ormObj).(AuthorityORMWithAfterDelete_); ok {
		err = hook.AfterDelete_(ctx, db)
	}
	return err
}

type AuthorityORMWithBeforeDelete_ interface {
	BeforeDelete_(context.Context, *gorm.DB) (*gorm.DB, error)
}
type AuthorityORMWithAfterDelete_ interface {
	AfterDelete_(context.Context, *gorm.DB) error
}

func DefaultDeleteAuthoritySet(ctx context.Context, in []*Authority, db *gorm.DB) error {
	if in == nil {
		return errors.NilArgumentError
	}
	var err error
	keys := []uint64{}
	for _, obj := range in {
		ormObj, err := obj.ToORM(ctx)
		if err != nil {
			return err
		}
		if ormObj.Id == 0 {
			return errors.EmptyIdError
		}
		keys = append(keys, ormObj.Id)
	}
	if hook, ok := (interface{}(&AuthorityORM{})).(AuthorityORMWithBeforeDeleteSet); ok {
		if db, err = hook.BeforeDeleteSet(ctx, in, db); err != nil {
			return err
		}
	}
	err = db.Where("id in (?)", keys).Delete(&AuthorityORM{}).Error
	if err != nil {
		return err
	}
	if hook, ok := (interface{}(&AuthorityORM{})).(AuthorityORMWithAfterDeleteSet); ok {
		err = hook.AfterDeleteSet(ctx, in, db)
	}
	return err
}

type AuthorityORMWithBeforeDeleteSet interface {
	BeforeDeleteSet(context.Context, []*Authority, *gorm.DB) (*gorm.DB, error)
}
type AuthorityORMWithAfterDeleteSet interface {
	AfterDeleteSet(context.Context, []*Authority, *gorm.DB) error
}

// DefaultStrictUpdateAuthority clears / replaces / appends first level 1:many children and then executes a gorm update call
func DefaultStrictUpdateAuthority(ctx context.Context, in *Authority, db *gorm.DB) (*Authority, error) {
	if in == nil {
		return nil, fmt.Errorf("Nil argument to DefaultStrictUpdateAuthority")
	}
	ormObj, err := in.ToORM(ctx)
	if err != nil {
		return nil, err
	}
	lockedRow := &AuthorityORM{}
	db.Model(&ormObj).Set("gorm:query_option", "FOR UPDATE").Where("id=?", ormObj.Id).First(lockedRow)
	if hook, ok := interface{}(&ormObj).(AuthorityORMWithBeforeStrictUpdateCleanup); ok {
		if db, err = hook.BeforeStrictUpdateCleanup(ctx, db); err != nil {
			return nil, err
		}
	}
	if hook, ok := interface{}(&ormObj).(AuthorityORMWithBeforeStrictUpdateSave); ok {
		if db, err = hook.BeforeStrictUpdateSave(ctx, db); err != nil {
			return nil, err
		}
	}
	if err = db.Save(&ormObj).Error; err != nil {
		return nil, err
	}
	if hook, ok := interface{}(&ormObj).(AuthorityORMWithAfterStrictUpdateSave); ok {
		if err = hook.AfterStrictUpdateSave(ctx, db); err != nil {
			return nil, err
		}
	}
	pbResponse, err := ormObj.ToPB(ctx)
	if err != nil {
		return nil, err
	}
	return &pbResponse, err
}

type AuthorityORMWithBeforeStrictUpdateCleanup interface {
	BeforeStrictUpdateCleanup(context.Context, *gorm.DB) (*gorm.DB, error)
}
type AuthorityORMWithBeforeStrictUpdateSave interface {
	BeforeStrictUpdateSave(context.Context, *gorm.DB) (*gorm.DB, error)
}
type AuthorityORMWithAfterStrictUpdateSave interface {
	AfterStrictUpdateSave(context.Context, *gorm.DB) error
}

// DefaultPatchAuthority executes a basic gorm update call with patch behavior
func DefaultPatchAuthority(ctx context.Context, in *Authority, updateMask *field_mask.FieldMask, db *gorm.DB) (*Authority, error) {
	if in == nil {
		return nil, errors.NilArgumentError
	}
	var pbObj Authority
	var err error
	if hook, ok := interface{}(&pbObj).(AuthorityWithBeforePatchRead); ok {
		if db, err = hook.BeforePatchRead(ctx, in, updateMask, db); err != nil {
			return nil, err
		}
	}
	pbReadRes, err := DefaultReadAuthority(ctx, &Authority{Id: in.GetId()}, db)
	if err != nil {
		return nil, err
	}
	pbObj = *pbReadRes
	if hook, ok := interface{}(&pbObj).(AuthorityWithBeforePatchApplyFieldMask); ok {
		if db, err = hook.BeforePatchApplyFieldMask(ctx, in, updateMask, db); err != nil {
			return nil, err
		}
	}
	if _, err := DefaultApplyFieldMaskAuthority(ctx, &pbObj, in, updateMask, "", db); err != nil {
		return nil, err
	}
	if hook, ok := interface{}(&pbObj).(AuthorityWithBeforePatchSave); ok {
		if db, err = hook.BeforePatchSave(ctx, in, updateMask, db); err != nil {
			return nil, err
		}
	}
	pbResponse, err := DefaultStrictUpdateAuthority(ctx, &pbObj, db)
	if err != nil {
		return nil, err
	}
	if hook, ok := interface{}(pbResponse).(AuthorityWithAfterPatchSave); ok {
		if err = hook.AfterPatchSave(ctx, in, updateMask, db); err != nil {
			return nil, err
		}
	}
	return pbResponse, nil
}

type AuthorityWithBeforePatchRead interface {
	BeforePatchRead(context.Context, *Authority, *field_mask.FieldMask, *gorm.DB) (*gorm.DB, error)
}
type AuthorityWithBeforePatchApplyFieldMask interface {
	BeforePatchApplyFieldMask(context.Context, *Authority, *field_mask.FieldMask, *gorm.DB) (*gorm.DB, error)
}
type AuthorityWithBeforePatchSave interface {
	BeforePatchSave(context.Context, *Authority, *field_mask.FieldMask, *gorm.DB) (*gorm.DB, error)
}
type AuthorityWithAfterPatchSave interface {
	AfterPatchSave(context.Context, *Authority, *field_mask.FieldMask, *gorm.DB) error
}

// DefaultPatchSetAuthority executes a bulk gorm update call with patch behavior
func DefaultPatchSetAuthority(ctx context.Context, objects []*Authority, updateMasks []*field_mask.FieldMask, db *gorm.DB) ([]*Authority, error) {
	if len(objects) != len(updateMasks) {
		return nil, fmt.Errorf(errors.BadRepeatedFieldMaskTpl, len(updateMasks), len(objects))
	}

	results := make([]*Authority, 0, len(objects))
	for i, patcher := range objects {
		pbResponse, err := DefaultPatchAuthority(ctx, patcher, updateMasks[i], db)
		if err != nil {
			return nil, err
		}

		results = append(results, pbResponse)
	}

	return results, nil
}

// DefaultApplyFieldMaskAuthority patches an pbObject with patcher according to a field mask.
func DefaultApplyFieldMaskAuthority(ctx context.Context, patchee *Authority, patcher *Authority, updateMask *field_mask.FieldMask, prefix string, db *gorm.DB) (*Authority, error) {
	if patcher == nil {
		return nil, nil
	} else if patchee == nil {
		return nil, errors.NilArgumentError
	}
	var err error
	var updatedCreatedAt bool
	var updatedUpdatedAt bool
	for i, f := range updateMask.Paths {
		if !updatedCreatedAt && strings.HasPrefix(f, prefix+"CreatedAt.") {
			if patcher.CreatedAt == nil {
				patchee.CreatedAt = nil
				continue
			}
			if patchee.CreatedAt == nil {
				patchee.CreatedAt = &timestamppb.Timestamp{}
			}
			childMask := &field_mask.FieldMask{}
			for j := i; j < len(updateMask.Paths); j++ {
				if trimPath := strings.TrimPrefix(updateMask.Paths[j], prefix+"CreatedAt."); trimPath != updateMask.Paths[j] {
					childMask.Paths = append(childMask.Paths, trimPath)
				}
			}
			if err := gorm1.MergeWithMask(patcher.CreatedAt, patchee.CreatedAt, childMask); err != nil {
				return nil, nil
			}
		}
		if f == prefix+"CreatedAt" {
			updatedCreatedAt = true
			patchee.CreatedAt = patcher.CreatedAt
			continue
		}
		if !updatedUpdatedAt && strings.HasPrefix(f, prefix+"UpdatedAt.") {
			if patcher.UpdatedAt == nil {
				patchee.UpdatedAt = nil
				continue
			}
			if patchee.UpdatedAt == nil {
				patchee.UpdatedAt = &timestamppb.Timestamp{}
			}
			childMask := &field_mask.FieldMask{}
			for j := i; j < len(updateMask.Paths); j++ {
				if trimPath := strings.TrimPrefix(updateMask.Paths[j], prefix+"UpdatedAt."); trimPath != updateMask.Paths[j] {
					childMask.Paths = append(childMask.Paths, trimPath)
				}
			}
			if err := gorm1.MergeWithMask(patcher.UpdatedAt, patchee.UpdatedAt, childMask); err != nil {
				return nil, nil
			}
		}
		if f == prefix+"UpdatedAt" {
			updatedUpdatedAt = true
			patchee.UpdatedAt = patcher.UpdatedAt
			continue
		}
		if f == prefix+"Id" {
			patchee.Id = patcher.Id
			continue
		}
		if f == prefix+"AuthorityName" {
			patchee.AuthorityName = patcher.AuthorityName
			continue
		}
		if f == prefix+"ParentId" {
			patchee.ParentId = patcher.ParentId
			continue
		}
		if f == prefix+"DefaultRouter" {
			patchee.DefaultRouter = patcher.DefaultRouter
			continue
		}
		if f == prefix+"MenuIds" {
			patchee.MenuIds = patcher.MenuIds
			continue
		}
	}
	if err != nil {
		return nil, err
	}
	return patchee, nil
}

// DefaultListAuthority executes a gorm list call
func DefaultListAuthority(ctx context.Context, db *gorm.DB, f *query.Filtering, s *query.Sorting, p *query.Pagination, fs *query.FieldSelection) ([]*Authority, error) {
	in := Authority{}
	ormObj, err := in.ToORM(ctx)
	if err != nil {
		return nil, err
	}
	if hook, ok := interface{}(&ormObj).(AuthorityORMWithBeforeListApplyQuery); ok {
		if db, err = hook.BeforeListApplyQuery(ctx, db, f, s, p, fs); err != nil {
			return nil, err
		}
	}
	db, err = gorm1.ApplyCollectionOperators(ctx, db, &AuthorityORM{}, &Authority{}, f, s, p, fs)
	if err != nil {
		return nil, err
	}
	if hook, ok := interface{}(&ormObj).(AuthorityORMWithBeforeListFind); ok {
		if db, err = hook.BeforeListFind(ctx, db, f, s, p, fs); err != nil {
			return nil, err
		}
	}
	db = db.Where(&ormObj)
	db = db.Order("id")
	ormResponse := []AuthorityORM{}
	if err := db.Find(&ormResponse).Error; err != nil {
		return nil, err
	}
	if hook, ok := interface{}(&ormObj).(AuthorityORMWithAfterListFind); ok {
		if err = hook.AfterListFind(ctx, db, &ormResponse, f, s, p, fs); err != nil {
			return nil, err
		}
	}
	pbResponse := []*Authority{}
	for _, responseEntry := range ormResponse {
		temp, err := responseEntry.ToPB(ctx)
		if err != nil {
			return nil, err
		}
		pbResponse = append(pbResponse, &temp)
	}
	return pbResponse, nil
}

type AuthorityORMWithBeforeListApplyQuery interface {
	BeforeListApplyQuery(context.Context, *gorm.DB, *query.Filtering, *query.Sorting, *query.Pagination, *query.FieldSelection) (*gorm.DB, error)
}
type AuthorityORMWithBeforeListFind interface {
	BeforeListFind(context.Context, *gorm.DB, *query.Filtering, *query.Sorting, *query.Pagination, *query.FieldSelection) (*gorm.DB, error)
}
type AuthorityORMWithAfterListFind interface {
	AfterListFind(context.Context, *gorm.DB, *[]AuthorityORM, *query.Filtering, *query.Sorting, *query.Pagination, *query.FieldSelection) error
}
type AuthorityServiceDefaultServer struct {
	DB *gorm.DB
}

// Create ...
func (m *AuthorityServiceDefaultServer) Create(ctx context.Context, in *CreateAuthorityRequest) (*CreateAuthorityResponse, error) {
	db := m.DB
	if custom, ok := interface{}(in).(AuthorityServiceAuthorityWithBeforeCreate); ok {
		var err error
		if db, err = custom.BeforeCreate(ctx, db); err != nil {
			return nil, err
		}
	}
	res, err := DefaultCreateAuthority(ctx, in.GetPayload(), db)
	if err != nil {
		return nil, err
	}
	out := &CreateAuthorityResponse{Result: res}
	if custom, ok := interface{}(in).(AuthorityServiceAuthorityWithAfterCreate); ok {
		var err error
		if err = custom.AfterCreate(ctx, out, db); err != nil {
			return nil, err
		}
	}
	return out, nil
}

// AuthorityServiceAuthorityWithBeforeCreate called before DefaultCreateAuthority in the default Create handler
type AuthorityServiceAuthorityWithBeforeCreate interface {
	BeforeCreate(context.Context, *gorm.DB) (*gorm.DB, error)
}

// AuthorityServiceAuthorityWithAfterCreate called before DefaultCreateAuthority in the default Create handler
type AuthorityServiceAuthorityWithAfterCreate interface {
	AfterCreate(context.Context, *CreateAuthorityResponse, *gorm.DB) error
}

// Read ...
func (m *AuthorityServiceDefaultServer) Read(ctx context.Context, in *ReadAuthorityRequest) (*ReadAuthorityResponse, error) {
	db := m.DB
	if custom, ok := interface{}(in).(AuthorityServiceAuthorityWithBeforeRead); ok {
		var err error
		if db, err = custom.BeforeRead(ctx, db); err != nil {
			return nil, err
		}
	}
	res, err := DefaultReadAuthority(ctx, &Authority{Id: in.GetId()}, db)
	if err != nil {
		return nil, err
	}
	out := &ReadAuthorityResponse{Result: res}
	if custom, ok := interface{}(in).(AuthorityServiceAuthorityWithAfterRead); ok {
		var err error
		if err = custom.AfterRead(ctx, out, db); err != nil {
			return nil, err
		}
	}
	return out, nil
}

// AuthorityServiceAuthorityWithBeforeRead called before DefaultReadAuthority in the default Read handler
type AuthorityServiceAuthorityWithBeforeRead interface {
	BeforeRead(context.Context, *gorm.DB) (*gorm.DB, error)
}

// AuthorityServiceAuthorityWithAfterRead called before DefaultReadAuthority in the default Read handler
type AuthorityServiceAuthorityWithAfterRead interface {
	AfterRead(context.Context, *ReadAuthorityResponse, *gorm.DB) error
}

// Update ...
func (m *AuthorityServiceDefaultServer) Update(ctx context.Context, in *UpdateAuthorityRequest) (*UpdateAuthorityResponse, error) {
	var err error
	var res *Authority
	db := m.DB
	if custom, ok := interface{}(in).(AuthorityServiceAuthorityWithBeforeUpdate); ok {
		var err error
		if db, err = custom.BeforeUpdate(ctx, db); err != nil {
			return nil, err
		}
	}
	res, err = DefaultStrictUpdateAuthority(ctx, in.GetPayload(), db)
	if err != nil {
		return nil, err
	}
	out := &UpdateAuthorityResponse{Result: res}
	if custom, ok := interface{}(in).(AuthorityServiceAuthorityWithAfterUpdate); ok {
		var err error
		if err = custom.AfterUpdate(ctx, out, db); err != nil {
			return nil, err
		}
	}
	return out, nil
}

// AuthorityServiceAuthorityWithBeforeUpdate called before DefaultUpdateAuthority in the default Update handler
type AuthorityServiceAuthorityWithBeforeUpdate interface {
	BeforeUpdate(context.Context, *gorm.DB) (*gorm.DB, error)
}

// AuthorityServiceAuthorityWithAfterUpdate called before DefaultUpdateAuthority in the default Update handler
type AuthorityServiceAuthorityWithAfterUpdate interface {
	AfterUpdate(context.Context, *UpdateAuthorityResponse, *gorm.DB) error
}

// List ...
func (m *AuthorityServiceDefaultServer) List(ctx context.Context, in *ListAuthorityRequest) (*ListAuthorityResponse, error) {
	db := m.DB
	if custom, ok := interface{}(in).(AuthorityServiceAuthorityWithBeforeList); ok {
		var err error
		if db, err = custom.BeforeList(ctx, db); err != nil {
			return nil, err
		}
	}
	pagedRequest := false
	if in.GetPaging().GetLimit() >= 1 {
		in.Paging.Limit++
		pagedRequest = true
	}
	res, err := DefaultListAuthority(ctx, db, in.Filter, in.OrderBy, in.Paging, in.Fields)
	if err != nil {
		return nil, err
	}
	var resPaging *query.PageInfo
	if pagedRequest {
		var offset int32
		var size int32 = int32(len(res))
		if size == in.GetPaging().GetLimit() {
			size--
			res = res[:size]
			offset = in.GetPaging().GetOffset() + size
		}
		resPaging = &query.PageInfo{Offset: offset}
	}
	out := &ListAuthorityResponse{Results: res, PageInfo: resPaging}
	if custom, ok := interface{}(in).(AuthorityServiceAuthorityWithAfterList); ok {
		var err error
		if err = custom.AfterList(ctx, out, db); err != nil {
			return nil, err
		}
	}
	return out, nil
}

// AuthorityServiceAuthorityWithBeforeList called before DefaultListAuthority in the default List handler
type AuthorityServiceAuthorityWithBeforeList interface {
	BeforeList(context.Context, *gorm.DB) (*gorm.DB, error)
}

// AuthorityServiceAuthorityWithAfterList called before DefaultListAuthority in the default List handler
type AuthorityServiceAuthorityWithAfterList interface {
	AfterList(context.Context, *ListAuthorityResponse, *gorm.DB) error
}

// Delete ...
func (m *AuthorityServiceDefaultServer) Delete(ctx context.Context, in *DeleteAuthorityRequest) (*DeleteAuthorityResponse, error) {
	db := m.DB
	if custom, ok := interface{}(in).(AuthorityServiceAuthorityWithBeforeDelete); ok {
		var err error
		if db, err = custom.BeforeDelete(ctx, db); err != nil {
			return nil, err
		}
	}
	err := DefaultDeleteAuthority(ctx, &Authority{Id: in.GetId()}, db)
	if err != nil {
		return nil, err
	}
	out := &DeleteAuthorityResponse{}
	if custom, ok := interface{}(in).(AuthorityServiceAuthorityWithAfterDelete); ok {
		var err error
		if err = custom.AfterDelete(ctx, out, db); err != nil {
			return nil, err
		}
	}
	return out, nil
}

// AuthorityServiceAuthorityWithBeforeDelete called before DefaultDeleteAuthority in the default Delete handler
type AuthorityServiceAuthorityWithBeforeDelete interface {
	BeforeDelete(context.Context, *gorm.DB) (*gorm.DB, error)
}

// AuthorityServiceAuthorityWithAfterDelete called before DefaultDeleteAuthority in the default Delete handler
type AuthorityServiceAuthorityWithAfterDelete interface {
	AfterDelete(context.Context, *DeleteAuthorityResponse, *gorm.DB) error
}
